package com.woniuxy.microfixeddepositserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.bean.bank.BankBean;
import com.woniuxy.microfixeddepositserver.dao.BankDao;
import com.woniuxy.microfixeddepositserver.service.IBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: masterhai
 * @date: 2021/4/21:10:30
 * @description:
 */
@RefreshScope
@Api("银行操作功能接口")
@RestController
@Slf4j
@RequestMapping(value = "/bank", produces = MediaType.APPLICATION_JSON_VALUE)
public class BankController {
    @Resource
    private IBankService bankService;

    /**
     * 分页条件银行信息
     *
     * @return
     */
    @ApiOperation("分页条件查询银行信息")
    @PostMapping("selectAll/{page}/{limit}")
    public ResultBean selectAllQuery (@PathVariable("page") Integer page,
                                  @PathVariable("limit") Integer limit,
                                  @ApiParam("查询对象") @RequestBody BankBean bankBean) {
        PageInfo<BankBean> bankBeanPageInfo = null;
        if (bankBean == null || StringUtils.isEmpty(bankBean.getBankName())) {
            //直接查询
            bankBeanPageInfo = bankService.selectALL(page, limit);
        } else {
            //条件查询
            bankBeanPageInfo = bankService.selectALLByQuery(page, limit, bankBean.getBankName());
        }
        return ResultBean.builder().code(200).msg("查询数据成功").data(bankBeanPageInfo).build();
    }


    /**
     * 修改银行信息
     */
    @ApiOperation("修改银行信息")
    @PutMapping("update")
    public ResultBean updateByPrimaryKey (@ApiParam("银行信息对象") @RequestBody BankBean bankBean) {
        if (ObjectUtils.isEmpty(bankBean)) {
            return ResultBean.ParamError();
        }
        int row = bankService.updateBank(bankBean);
        return ResultBean.builder().code(200).msg(row > 0 ? "修改数据成功" : "修改数据失败").build();
    }

    /**
     * 删除银行信息
     */
    @ApiOperation("删除银行信息")
    @GetMapping("delete/{id}")
    public ResultBean deleteBank (@PathVariable("id") Integer id) {
        if (id == null || "".equals(id)) {
            return ResultBean.ParamError();
        }
        int row = bankService.deleteBank(id);
        return ResultBean.builder().code(200).msg(row > 0 ? "删除数据成功" : "删除数据失败").build();
    }

    /**
     * 新增银行信息
     */
    @ApiOperation("新增银行信息")
    @PutMapping("add")
    public ResultBean addBank (@ApiParam("定期理财产品对象") @RequestBody BankBean bankBean) {
        if (ObjectUtils.isEmpty(bankBean)) {
            return ResultBean.ParamError();
        }
        int row = bankService.addBank(bankBean);
        return ResultBean.builder().code(200).msg(row > 0 ? "新增数据成功" : "新增数据失败").build();
    }

    @ApiOperation("查询单个银行信息")
    @GetMapping("getOne/{id}")
    public ResultBean getBankById (@ApiParam("定期理财产品对象") @PathVariable Integer id) {
        if (id == null || "".equals(id)) {
            return ResultBean.ParamError();
        }
        BankBean bankBean = bankService.getBankById(id);
        return ResultBean.builder().code(200).msg("查询银行数据成功").data(bankBean).build();
    }

}
